package com.util;

import com.google.gson.JsonObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @Author: 柏竹
 * @Description: IP工具 (多余功能)
 * @Date_Created_in: 2021-05-25 14:12
 * @Modified_By: Sans
 * @Project: 快递e栈
 */
public class IpUtil {
    
    private static final String KEY = "ea2e5f23d8fb353c6416002a96f38933";
    
    /**
     * 获取本终端的IP地址
     * @return
     */
    public static String getOutIPV4() {
        String ip = "";
        String chinaz = "http://ip.chinaz.com";
        
        StringBuilder inputLine = new StringBuilder();
        String read = "";
        URL url = null;
        HttpURLConnection urlConnection = null;
        BufferedReader in = null;
        try {
            url = new URL(chinaz);
            urlConnection = (HttpURLConnection) url.openConnection();
            in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
            while ((read = in.readLine()) != null) {
                inputLine.append(read + "\r\n");
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        Pattern p = Pattern.compile("\\<dd class\\=\"fz24\">(.*?)\\<\\/dd>");
        Matcher m = p.matcher(inputLine.toString());
        if (m.find()) {
            String ipstr = m.group(1);
            ip = ipstr;
        }
        return ip;
    }
    
    /**
     * 查 城市
     * @param ip 公网ip地址
     * @return 城市
     */
    public static String findByCity(String ip) {
        String data = null;
        String city = null;
        String Url = "http://apis.juhe.cn/ip/ipNew?ip="+ip+"&key="+KEY;
        try {
            //1.      创建新的URL类对象
            URL url= new URL(Url);
            //2.      通过URL对象打开网络连接，并连接对象conn
            URLConnection conn = url.openConnection();
            //3.      连接对象，得到用于读取网页内容的输入流is
            InputStream is = conn.getInputStream();
            //4.     以UTF-8形式转化字节输出流
            BufferedReader br = new BufferedReader(new InputStreamReader(is , "UTF-8"));
            //5.      读取流中的一行内容
            data = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        if (data != null) {
            JsonObject jsonObject = JSONUtil.toString(data);
            //data : {"resultcode":"200","reason":"查询成功","result":{"Country":"中国","Province":"广西","City":"崇左市","Isp":"电信"},"error_code":0}
            JsonObject result = jsonObject.get("result").getAsJsonObject();
            city = result.get("City").getAsString();
        }
        return city;
    }
    
}
